package mortal.game.control
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import com.gengine.utils.*;
    import modules.*;
    import mortal.game.manager.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.view.activity.*;
    import mortal.game.view.chat.*;
    import mortal.game.view.chat.chatViewData.*;
    import mortal.game.view.uiIconBtn.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class ActivityController extends Controller
    {
        private var _transport:ActivityTransportModule;
        private var _transportIcon:TransportActHintIcon;
        private var _activityTimer:SecTimer;
        private var _transportInfo:String;
        private var _transportChat:String;

        public function ActivityController()
        {
            
        }

        private function onCampTransportInfoRes(param1:MessageBlock) : void
        {
            cache.activity.transport = param1.messageBase as SCampTransportInfo;
            switch(cache.activity.transport.flag)
            {
                case 0:
                {
                    break;
                }
                case 1:
                {
                    this.onActivityTransportStart();
                    break;
                }
                case 2:
                {
                    this.onActivityTransportEnd();
                    break;
                }
                case 3:
                {
                    break;
                }
                default:
                {
                    break;
                }
            }
            
        }

        private function onTaskGetRes(param1:Object) : void
        {
            if (param1 == null)
            {
                
            }
            var _loc_2:* = param1.task as TaskInfo;
            if (_loc_2 != null)
            {
                if (_loc_2.sTask.group == ETaskGroup._ETaskGroupTransport)
                {
                    if (this._transportIcon && this._transportIcon.parent)
                    {
                        this._transportIcon.parent.removeChild(this._transportIcon);
                        EffectManager.glowFilterUnReg(this._transportIcon);
                        this._transportIcon.dispatchEvent(new UIIconEvent(UIIconEvent.RemoveIcon));
                    }
                    if (this._transport && !this._transport.isHide)
                    {
                        this._transport.hide();
                    }
                }
            }
            
        }

        private function onTaskEndRes(param1:Object) : void
        {
            if (!cache.activity.transport || cache.activity.transport.flag != 1)
            {
                
            }
            if (param1 == null)
            {
                
            }
            var _loc_2:* = param1.task as TaskInfo;
            if (_loc_2 != null)
            {
                if (_loc_2.sTask.group == ETaskGroup._ETaskGroupTransport)
                {
                    if (cache.daily.transportInfo.num != cache.daily.transportInfo.totalNum)
                    {
                        if (!this._transportIcon)
                        {
                            this._transportIcon = new TransportActHintIcon();
                            this._transportIcon.addEventListener(UIIconEvent.RemoveIcon, this.onRemoveIconHandler);
                        }
                        if (!this._transportIcon.parent)
                        {
                            UiHintIconView.instance.addHintIcon(this._transportIcon);
                        }
                    }
                }
            }
            
        }

        public function onActivityTransportStart() : void
        {
            if (cache.task.getCanGetTaskByGroup(ETaskGroup._ETaskGroupTransport).length != 0)
            {
                if (!this._transportIcon)
                {
                    this._transportIcon = new TransportActHintIcon();
                    this._transportIcon.addEventListener(UIIconEvent.RemoveIcon, this.onRemoveIconHandler);
                }
                UiHintIconView.instance.addHintIcon(this._transportIcon);
            }
            this._transportChat = "<MsgObj>COLOR,4," + cache.activity.transportStartTime + "~" + cache.activity.transportEndTime + "</MsgObj>" + "为" + "<MsgObj>COLOR,3,灵运时间</MsgObj>" + "，在灵运时段" + "<MsgObj>TRAN,</MsgObj>" + "可额外获得更多的" + "<MsgObj>COLOR,6,奖励</MsgObj>" + "喔~！";
            this._transportInfo = HTMLUtil.addColor(cache.activity.transportStartTime + "~" + cache.activity.transportEndTime, "#ff00ff");
            this._transportInfo = this._transportInfo + "为";
            this._transportInfo = this._transportInfo + HTMLUtil.addColor("灵运时间", "#00fffff");
            this._transportInfo = this._transportInfo + "，在灵运时段";
            this._transportInfo = this._transportInfo + HTMLUtil.addColor("护送灵兽", "#ff00ff");
            this._transportInfo = this._transportInfo + ("可额外获得更多的" + HTMLUtil.addColor("奖励", "#ff0000") + "喔~！");
            this._transportInfo = HTMLUtil.addColor(this._transportInfo, "#ffffff");
            this.onTimerHandler(null);
            this.startTimer();
            
        }

        private function onRemoveIconHandler(event:UIIconEvent) : void
        {
            if (!this._transport)
            {
                this._transport = new ActivityTransportModule();
                this._transport.layer = LayerManager.windowLayer;
            }
            this._transport.transportStart();
            this._transport.show();
            
        }

        private function onActivityTransportEnd() : void
        {
            var _loc_1:* = HTMLUtil.addColor("灵运活动已经结束", "#ffffff");
            MsgManager.addBroadCast(_loc_1);
            GameController.chat.addTipMsg("灵运活动已经结束。", ChatType.Tips, ChatArea.World, 2);
            if (this._transportIcon && this._transportIcon.parent)
            {
                this._transportIcon.parent.removeChild(this._transportIcon);
                EffectManager.glowFilterUnReg(this._transportIcon);
                this._transportIcon.dispatchEvent(new UIIconEvent(UIIconEvent.RemoveIcon));
            }
            if (this._transport && !this._transport.isHide)
            {
                this._transport.hide();
            }
            this.endTimer();
            
        }

        private function startTimer() : void
        {
            if (!this._activityTimer)
            {
                this._activityTimer = new SecTimer(600);
                this._activityTimer.addListener(TimerType.ENTERFRAME, this.onTimerHandler);
            }
            if (!this._activityTimer.running)
            {
                this._activityTimer.start();
            }
            
        }

        private function endTimer() : void
        {
            if (this._activityTimer && this._activityTimer.running)
            {
                this._activityTimer.stop();
                this._activityTimer.isDelete = false;
            }
            
        }

        private function onTimerHandler(param1:SecTimer) : void
        {
            GameController.chat.addTipMsg(this._transportChat, ChatType.Tips, ChatArea.World, 2);
            MsgManager.addBroadCast(this._transportInfo);
            
        }

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCampTransportInfo, this.onCampTransportInfoRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskGetRes, this.onTaskGetRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskEndRes, this.onTaskEndRes);
            
        }

        override protected function initView() : IView
        {
            return super.initView();
        }

    }
}
